/****************************************************
          File Name:12-4_imply.cpp
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Fri 26 Dec 2014 03:23:30 PM CST
*****************************************************/

#include"12-4.h"

Stack::Stack(int n)
{
    using std::cout;
    if(n > MAX)
        cout << "too large, quit now\n";
    else if(n <= 0)
        cout << "can NOT be 0 or negative\n";
    else
        size = n;
    pitems = new Item[size];
    top = 0;
}

Stack::Stack(const Stack & st)
{
    size = st.size;
    pitems = new Item[size];
    top = 0;
}

Stack::~Stack()
{
    delete [] pitems;
}

bool Stack::isempty() const
{
    return top == 0;
}

bool Stack::isfull() const
{
    return top == size;
}

bool Stack::push(const Item & item)
{
   if(top < size)
   {
       pitems[top++] = item;
       return true;
   } 
   else
       return false;
}

bool Stack::pop(Item & item)
{
    if(top > 0)
    {
        item = pitems[--top];
        return true;
    }
    else
        return false;
}

Stack & Stack::operator=(const Stack & st)
{
    if(this == &st)
        return *this;
    delete [] pitems;

    size_t len = sizeof(st.pitems) + 1;         // why plus 1?
    pitems = new Item[len];

    for(int i=0; i<len; i++)
        pitems[i] = st.pitems[i];
    top = len;

    return *this;
}
